# -*- coding:utf-8 -*-
"""
Python 3.7
Author：刘爽、何若溥
Time：2022/08/23
"""

import pymysql


class MysqlUntil:

    def __init__(self, db_host, db_user, db_password, db_port, db):
        try:
            self.conn = pymysql.connect(host=db_host, user=db_user, password=db_password, port=db_port, db=db)
            self.cursor = self.conn.cursor()
        except Exception as e:
            print("数据库连接失败，报错如下：\n", e)

    def searchData(self, sql):
        try:
            self.cursor.execute(sql)
            data = self.cursor.fetchall()
            # print("数据查询成功")
            return data
        except Exception as e:
            print("数据库查询失败，报错如下：\n", e)

    def closeDB(self):
        try:
            self.cursor.close()
            self.conn.close()
        except Exception as e:
            print("数据库关闭失败，报错如下：\n", e)
